define(['app'], function(app) {
	'use strict';

	var Infos = null;
	var nowYear = (new Date()).getFullYear();
	var PAY_METHOD = [3, 4];//3微信	 4支付宝

	var controller = function controller($scope, Predeposit, GoPage, Auth, GoDialog, GoPay) {
		
		$scope.data = {};
		$scope.paymethod = [true, false];
		$scope.data.in_paymethod = PAY_METHOD[0];//默认微信支付
		$scope.data.accinfos = [];

		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			Infos = Auth.get_loginInfo();
			if (!Infos.user_id) {
				GoDialog.alert("提示",'请先登录',function(){
					GoPage.go_states('t-login');
				});
				return;
			}
		}, false);

		// 充值支付方式
		$scope.paymethodChanged = function(index) {
			for (var i = 0; i < $scope.paymethod.length; i++) {//控制选中状态
				$scope.paymethod[i] = (i == index);
			}
			$scope.data.in_paymethod = PAY_METHOD[index];
		}

		$scope.accinfoChanged = function(id) {
			for (var i = 0; i < $scope.data.accinfos.length; i++) {
				$scope.data.accinfos[i].selected = (id == $scope.data.accinfos[i].id);
			}
			$scope.data.sel_accinfo = id;
		}

		$scope.init_page = function() {
			if (!Infos) return;
			var param = {
				year: nowYear,
				session: Infos.user_id
			}

			Predeposit.search(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				var result = res.result;

				$scope.data.total_advance = result.total_advance;
				function fn(input) {
					try {
						if (typeof input == 'string') {
							var i = input.indexOf('.');
							if (i > 0) {
								var point = input.substr(i+1);
								if (point && point.length > 2) {
									return input.substr(0, i+3);
								}
							}
						}

					} catch(ex) {}

					return input;
				}

				$scope.data.total_advance = fn($scope.data.total_advance);

				$scope.data.withdrawals = result.withdrawals || [];

				for (var i=0; i<$scope.data.withdrawals.length; i++) {
					var row = $scope.data.withdrawals[i];
					if (row.remit == '1') {
						row.status_text = '已汇款';
					} else if (row.auditing == '2') {
						row.status_warning = true;
						row.status_text = '申请拒绝：'+row.auditing_msg;
					} else {
						row.status_text = '处理中...';
					}
				}
			})

			Predeposit.getAccinfos({
				session: Infos.user_id
			}, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}
				$scope.data.accinfos = res.result;
			})
		}
		
		// 提现
		$scope.withdraw = function() {
			if (!Infos) return;

			var out_money = Number($scope.data.out_money);
			if (isNaN(out_money) || !out_money || out_money <= 0 || out_money > 5000) {
				GoDialog.toast("请输入正确的提现金额"); return;
			}
			if($scope.data.accinfos==null||$scope.data.accinfos.length==0){
				GoDialog.toast("请先绑定银行卡"); 
				return;
			}
			if (!$scope.data.sel_accinfo) {
				GoDialog.toast("请选择提现的银行卡"); 
				return;
			}

			var param = {
				session: Infos.user_id,
				cash: $scope.data.out_money,
//				memo: ($scope.data.out_memo || '').trim(),
				accinfos: $scope.data.sel_accinfo
			}

			Predeposit.withdraw(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				$scope.data.sel_accinfo = null;
				$scope.data.out_money = "";
//				$scope.data.out_memo = "";

				GoDialog.toast("提现申请成功");
				GoPage.go_back();
			})
		}

		// 充值
		$scope.recharge = function() {
			if (!Infos) return;
			if (!($scope.data.in_money - 0) || $scope.data.in_money <= 0) {
				GoDialog.toast("请输入正确的充值金额"); return;
			}
			if (!$scope.data.in_paymethod) {
				GoDialog.toast("请选择充值方式"); return;
			}
			Predeposit.get_rechargepayment({
				'amount': $scope.data.in_money,
				'pay_app_id': $scope.data.in_paymethod
			}, function(res) {
				if (!res.success) {
//					GoDialog.alert("充值失败",res.msg);//:isSuccess/:payType/:money
					GoPage.go_states("predepositresult",{'isSuccess':false,'payType':$scope.data.in_paymethod,'money':$scope.data.in_money,'failReason':data.msg});
					return;
				}
				doRecharge(res.result.pay_fields);
			})
		}
		
		//do_pay: function(fields, pay_id, callOK, callFail) {})
		function doRecharge(pay_fields) {
			var pay_id = ($scope.data.in_paymethod == 3) ? 1 : 2;//1微信
			GoPay.do_pay(pay_fields, pay_id, function(data) {//callOK
				$scope.data.in_money = "";
				$scope.data.in_memo = "";
				GoPage.go_states("predepositresult",{'isSuccess':true,'payType':$scope.data.in_paymethod,'money':$scope.data.in_money});
			})
		}
		
	}

	controller.$inject = ['$scope', 'Predeposit', 'GoPage', 'Auth', 'GoDialog', 'GoPay'];
	
	return app.registerController("predepositctrl",controller);
});